package tank;

import frame.GameFrame;
import gameBasics.*;
import tank.fire.BulletType;
import tank.fire.DefaultFireStrategy;
import tank.fire.FireStrategy;
import tank.fire.FourDirFireStrategy;
import java.awt.*;
import java.util.Objects;
import java.util.Random;

public abstract class Tank extends GameObject {

    protected final TankType tankType;                //坦克种类
    protected final BulletType bulletType = BulletType.Default;          //坦克所持有的炮弹的种类
    public static final int WIDTH = ResourceManager.tankU.getWidth();    //坦克的宽度
    public static final int HEIGHT = ResourceManager.tankU.getHeight();  //坦克的高度
    protected Dir dir;                                //坦克运动方向
    protected static final int SPEED = Objects.requireNonNull(PropertyManager.getInt("tankSpeed"));   //坦克速度
    protected boolean livingState = true;             //坦克存活状态
    protected boolean movingState = false;            //坦克移动状态
    protected final Random random = new Random();

    //有参构造方法
    public Tank(TankType tankType, int x, int y, Dir dir){
        this.tankType = tankType;
        this.x = x;
        this.y = y;
        this.dir = dir;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;
    }

    public TankType getTankType() {
        return tankType;
    }

    @Override
    public int getWidth() {
        return WIDTH;
    }

    @Override
    public int getHeight() {
        return HEIGHT;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir){
        this.dir = dir;
    }

    public boolean isLivingState() {
        return livingState;
    }

    public void setLivingState(boolean livingState) {
        this.livingState = livingState;
    }

    public void setMovingState(boolean movingState) {
        this.movingState = movingState;
    }

    /**
     * 绘制坦克
     * @param graphics 画笔对象
     */
    public abstract void paint(Graphics graphics);

    /**
     * 坦克移动
     */
    public void move() {
        if(!movingState) return;

        switch (dir){
            case LEFT -> x -= SPEED;
            case UP -> y -= SPEED;
            case RIGHT -> x += SPEED;
            case DOWN -> y += SPEED;
        }

        //边界检测
        boundsCheck();

        //更新坦克的rectangle
        rectangle.x = this.x;
        rectangle.y = this.y;
    }

    /**
     * 坦克与窗口边界的碰撞检测
     */
    private void boundsCheck() {
        if(this.x < 0) x = 0;
        if(this.y < 30) y = 30;
        if(this.x > GameFrame.GAME_WIDTH - Tank.WIDTH) x = GameFrame.GAME_WIDTH - Tank.WIDTH;
        if(this.y > GameFrame.GAME_HEIGHT - Tank.HEIGHT) y = GameFrame.GAME_HEIGHT - Tank.HEIGHT;
    }

    /**
     * 坦克开火
     */
    public void fire() {
        FireStrategy fireStrategy;
        if(tankType == TankType.MAIN){
            switch (bulletType){
                case Default -> DefaultFireStrategy.getInstance().fire(this);
                case FourDir -> FourDirFireStrategy.getInstance().fire(this);
            }
        }else{
            fireStrategy = DefaultFireStrategy.getInstance();
            fireStrategy.fire(this);
            DefaultFireStrategy.getInstance().fire(this);
        }
    }

    /**
     * 坦克消亡
     */
    public void die() {
        this.livingState = false;
    }

}